﻿function jsSelectIsExitItem(objSelect, objItemValue) {
    var isExit = false;
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].value == objItemValue) {
            isExit = true;
            break;
        }
    }
    return isExit;
}


function jsAddItemToSelect(objSelect, objItemText, objItemValue) {
    //判断是否存在        
    if (!jsSelectIsExitItem(objSelect, objItemValue)) {
        var varItem = new Option(objItemText, objItemValue);
        objSelect.options.add(varItem);
    }
}

function jsRemoveItemFromSelect(objSelect, objItemValue) {
    //判断是否存在        
    if (jsSelectIsExitItem(objSelect, objItemValue)) {
        for (var i = 0; i < objSelect.options.length; i++) {
            if (objSelect.options[i].value == objItemValue) {
                objSelect.options.remove(i);
                break;
            }
        }
    }
}

function jsRemoveSelectedItemFromSelect(objSelect) {
    var length = objSelect.options.length - 1;
    for (var i = length; i >= 0; i--) {
        if (objSelect[i].selected == true) {
            objSelect.options[i] = null;
        }
    }
}


function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {
    //判断是否存在        
    if (jsSelectIsExitItem(objSelect, objItemValue)) {
        for (var i = 0; i < objSelect.options.length; i++) {
            if (objSelect.options[i].value == objItemValue) {
                objSelect.options[i].text = objItemText;
                break;
            }
        }
        alert("成功修改");
    } else {
        alert("该select中 不存在该项");
    }
}


function jsSelectItemByValue(objSelect, objItemText) {
    //判断是否存在        
    var isExit = false;
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].text == objItemText) {
            objSelect.options[i].selected = true;
            isExit = true;
            break;
        }
    }
    //Show出结果        
    if (isExit) {
        alert("成功选中");
    } else {
        alert("该select中 不存在该项");
    }
}

//批量删除数据
function RemoveItems(objSelect, array) {
    for (var i = 0; i < array.length; i++) {
        jsRemoveItemFromSelect(objSelect, array[i]);
    }
}

function jsRemoveSelectedItemFromSelect(objSelect) {
    var length = objSelect.options.length - 1;
    for (var i = length; i >= 0; i--) {
        if (objSelect[i].selected == true) {
            objSelect.options[i] = null;
        }
    }
}

function DropDownIsSelected(name) {
    var selected = false;
    var objSelect = document.getElementById(name);
    if (!objSelect)
        return selected;
    var length = objSelect.options.length - 1;
    for (var i = length; i >= 0; i--) {
        if (objSelect[i].selected && objSelect[i].value != "" && objSelect[i].value != null) {
            selected = true;
            break;
        }
    }
    return selected;
}

function RadioIsSelected(name) {
    var objSelect=document.getElementsByName(name);
    var selected = false;
    var length = objSelect.length - 1;
    for (var i = length; i >= 0; i--) {
        if (objSelect[i].checked && objSelect[i].value != "" && objSelect[i].value != null) {
            selected = true;
            break;
        }
    }
    return selected;
}

function ChkIsSelected(name) {
    var maxlength = 50;
    var selected = false;
    for (var i = 0; i < maxlength; i++) {
        var chkid = name + "_" + i;
        var chk=document.getElementById(chkid);
        if (chk) {
            if (chk.checked) {
                selected = true;
                break;
            }
        }
    }
    return selected;
}